package com.example.yls.demoa;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
 * Created by yls on 2019/5/17.
 */
public class MyView extends View {
    Paint paint=new Paint();
    private float center;
    private float radio;
    Canvas canvas1;
    Bitmap bitmap;
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint.setStyle(Paint.Style.STROKE);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    postInvalidate();
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (bitmap==null){
            bitmap=Bitmap.createBitmap(right,bottom,Bitmap.Config.ARGB_8888);
        }
        if (canvas1==null){
            canvas1=new Canvas(bitmap);
        }
    }

    public void drawBuf(){
        canvas1.drawColor(Color.WHITE);
        canvas1.drawCircle(center,center,100,paint);
        canvas1.drawCircle(200,200,radio,paint);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
       canvas.drawBitmap(bitmap,0,0,paint);

    }

    public void setCircle(float circle) {
        this.center = circle;
        drawBuf();
    }
    public void setRadio(float radio) {
        this.radio = radio;
        drawBuf();
    }
}
